Showing posts with label Multiple Choice Questions. Show all posts
Showing posts with label Multiple Choice Questions. Show all posts

Wednesday, 11 June 2014

MCQ #15

Question :

Consider the following  code : 

class Super { static String ID = "QBANK"; }
class Sub extends Super{
static { System.out.print("In Sub"); }
}
public class Test{
public static void main(String[] args){
System.out.println(Sub.ID);
}
}

What  will  be the output  when  class Test  is run ?

A. I t  will  print  In Sub and QBANK.
B. I t  will  print  QBANK.
C. Depends on  the implementation  of  JVM.
D. It  will  not  even  compile.
E. None of  the above.

Answer

Answer is option B.

Explanation 

It would look like the program would print  "In sub" followed by QBANK and the option would be A but it is not. 

Since ID is inherited from the Super class it is really the Super class that is initialized and not the Sub class. So that static method in Sub class will not get printed. 

A class is initialized only when it's content (variables/methods) which are not inherited gets referenced. Not otherwise.

From JLS 12.4.1:
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

Wednesday, 26 February 2014

MCQ #14

Question : 

There are two code snippets given below. You have to predict output for both.

First :

String str1="str";
String str2="ing";
String concat=str1+str2;

System.out.println(concat=="string");

Second :

final String str1="str";
final String str2="ing";
String concat=str1+str2;

System.out.println(concat=="string");

In both case answer will be either true or false.

Answer : 

Answer is 1st case is false.
Answer in 2nd case is true.

Explanation :

When Java code is compiled, compiler does some optimizations of it's own. An expression that is know to not change during runtime is know as compile time constant expression. So lets say if you have a String
String concat = "str" + "ing";
after compilation it becomes
String concat = "string";
So if question is framed like how many String instances are created in above java code then the answer is one. String literals are interned. So lets come to our problem

  1. (False)In first case Strings are not final. Also as we know String is immutable in nature. Concatenation of two String literals yields a new String instance and hence the answer on comparison would be false.
  2. (True)In second case Strings are defined as final. That equivalently means the Strings will not change at runtime and hence they form compile time constant expression. So when the code is compiled concat actually points to "string" literal(which is interned as all String literals are). Now since we are comparing this with another String literal with same content both will point to same String instance in the String pool. So the answer in this case would be true.   

Important Links

  1. Difference between using == operator and equals() method in Java
  2. Comparing strings with == which are declared final in Java
  3. how many java String objects will be created in the statement String s=“abc”+“xyz”

Sunday, 7 April 2013

MCQ #13

Question) Given this code what is the output?

class Exam
{
    protected String level = "Easy";

    public void printLevel()
     {
         System.out.println(level);
      }


Class IITExam extends Exam
{
     private String level = "Difficult";


 IITExam  iitExam = new IITExam();
 iitExam .printLevel();
 
Options)


  • Difficult
  • Easy
Answer) Easy

Explanation) Methods can be overridden not the attributes. Rest is self explanatory.

MCQ #12

Question) Given this code what is the output?

boolean b = false;
if(b = true)
{
   System.out.println("Yes");
}

Options)

  • Yes
  • Nothing is printed
Answer) Yes

Explanation) If you are puzzled looking at the output observe the code again. Inside the if condition you have an assignment operator(=) and not a equality operator(==) . Assignment will always be true regardless of anything and hence the code inside the for loop will always execute. Hence the output will be "Yes". Also note that b will be set to "True".

Saturday, 6 April 2013

MCQ #11

Question) Are you allowed to have more than one top level (non inner) class definition per source file?

Options)

  • No
  • Yes
Answer) Yes

Explanation) Yes you can have multiple top level class definitions per source file. Yeah, it's also true that the source file name must be same as that of the class name. To resolve both these constraints Java states some basic rules which are -
  1. Out of the multiple top level classes defined only one can be public.
  2. Source file name must match this public class name.

Saturday, 16 March 2013

MCQ #10

Question) If you do not use curly braces for a while loop body , what will execute if the while condition is true?

Options)

  • Nothing
  • First statement of while
Answer) A copy of the value

Explanation)   If you do not provide braces to any loop statements then 1st statement is taken by default. Same is true even for if-else statement For more details refer the post on Loop Statements.

MCQ #9

Question) When you pass variable as an argument to a method call, what are you passing?

Options)
  • The actual value
  • A copy of the value
Answer) A copy of the value

Explanation)   Java works that way - pass by copy. For more details refer the post on Classes in Java.

MCQ #8

Question) Assume x = 0 and y = 3. What is the value of x after :  x = y++;

Options)

  • 0
  • 3
  • 4
Answer) 3

Explanation)   You must know the difference between pre-increment (++x) and post-increment(x++)  operators before you can answer this question.

Difference between ++x and x++

       Pre-increment operator(++x) will first increment the value of x and then carry out any operations that are due whereas in post-increment(x++) operator , operation is first carried out and then x in incremented.
     Sounds a bit hard to digest?
       Lets take an example to ensure we understand it completely.

int x = 0;
int y = 3;

Lets consider pre-increment operator first(++y)

x = ++y;

Result of above will be x = 4 and y = 4 because pre-increment operator first increments the value i.e value becomes 5 and then carries out the = operation.

Now lets see the post-increment operator(y++)

x = y++;

 Result of above will be x = 3 and y = 4 because post-increment operator first carries out the = operation i.e assigns value 3 to x and then increments the value of y to 4.

       Our question represents the second case(post-increment). So our answer is 3.

MCQ #7

Question) Can you compare boolean to an integer?

Options)

  • True
  • False
Answer) False

Explanation)   In Java boolean has value true or false and not 1 or 0 or anything else. So you just can't do it(Watch out C folks!).

MCQ #6

Question) x = false; y = true; What is the result of x && y

Options)
  • True
  • False
Answer) False

Explanation)  This is a straightforward question. Lets revise the entire table once -

x         y          x&&y
true    true      true
true    false     false
false   true      false
false  false      false

So basically in  expression1&&(and)expression2, entire expression is false if any one of the expressions or both are false. On the other hand this evaluates to be true only when both expressions are true.

Our question represents 3rd row of the above table.The answer is therefore False. 

Saturday, 9 March 2013

MCQ #5

Question) In switch construct , the default statement must be placed after all the case statements.

Options)

  • True
  • False
Answer) False

Explanation) Go through the Java tutorial on switch statement to know more about switch statement and it's usage.As for our question, default statement can be put anywhere in the switch construct but it will be called only when none of the other cases match. Also since default statement is the last one to be checked we need not even write break statement in default statement.So the answer is clearly False.

MCQ #4

Question) Member (instance) variables are always assigned a default value if not explicitly initialized.

Options)

  • True
  • False
Answer) True

Explanation) Go through the Java tutorial on Objects in java to know the difference between instance variables and local variables.As for our question, yes instance variables are assigned default values if not explicitly initialized.On the other hand compiler will complaint if local variables are used without initialization.

MCQ #3

Question)Assume x = 6 and y = 7 .What is the result of (!(y == x))

Options)

  • True
  • False
Answer) True

Explanation) The answer is pretty straight forward . x=6  and y=7 so y == x is false. Negation ( ! ) of false is true and hence our final result is True. Remember brackets have higher preference than any of the operators.

MCQ #2

Question) Using a break in a for loop causes the loop to break out of the current execution and jump to next iteration.

Options)
  • True
  • False
Answer) Flase

Explanation) I would recommend go through the post on Transfer Statements in Java Tutorials section.break keyword will cause loop to break out of current execution permanently and continue with the code after the loop.Note that break statement is used only in loops and switch statement.So answer is False.

MCQ #1

Question) Methods which are marked protected can be accessed only by classes within the same package

Options)
  • True
  • False
Answer) Flase

Explanation) I would recommend go through the post on Access Modifiers in Java Tutorials section.If a member (be it a member variable or a member function) is defined to be protected then it is accessible by members in same class, in same package and also in all the sub classes of class in which the protected member is defined. So it is not just the same package.Hence answer is False.
t> UA-39527780-1 back to top